package com.fanfan.SmartPensionSystembackend.controller;

import com.fanfan.SmartPensionSystembackend.entity.Payment;
import com.fanfan.SmartPensionSystembackend.service.PaymentService;
import com.fanfan.SmartPensionSystembackend.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description PaymentController
 * @Author Guangdi Chen
 * @Date 2025-03-30
 */
@RestController
@RequestMapping("/payment")
@CrossOrigin
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    // 获取所有老人信息
    @GetMapping("/getAllElderInfo")
    public Map<String, Object> getAllElderInfo() {
        try {
            List<Payment> elderInfoList = paymentService.getAllElderInfo();
            if(elderInfoList != null && !elderInfoList.isEmpty()){
                return ResponseUtil.success("查询成功", elderInfoList);
            }else{
                return ResponseUtil.error("查询失败");
            }
        }catch (Exception e){
            return ResponseUtil.error("查询失败：" + e.getMessage());
        }
    }

    // 根据elderId获取老人信息
    @GetMapping("/getElderInfoByElderId")
    public Map<String, Object> getElderInfoByElderId(@RequestParam String elderId) {
        try {
            Payment elderInfo = paymentService.getElderInfoByElderId(elderId);
            if(elderInfo != null){
                return ResponseUtil.success("查询成功", elderInfo);
            }else{
                return ResponseUtil.error("用户不存在！");
            }
        }catch (Exception e){
            return ResponseUtil.error("查询失败：" + e.getMessage());
        }
    }

    // 一键插入所有老人的月固定费用
    @PostMapping("/pushFixedCost")
    public Map<String, Object> pushAllFixedCost(@RequestBody Map<String, Double> fixationFeeInfo) {
        try {
            int result = paymentService.pushAllFixedCost(fixationFeeInfo);
            if(result > 0){
                return ResponseUtil.success("插入成功", result);
            }else{
                return ResponseUtil.error("插入失败");
            }
        }catch (Exception e){
            return ResponseUtil.error("插入失败：" + e.getMessage());
        }
    }

    // 插入或更新特殊费用
    @PostMapping("/pushSpecialCost")
    public Map<String, Object> insertOrUpdateSpecialCost(@RequestBody Map<String, Object> specialCostInfo) {
        try {
            int result = paymentService.insertOrUpdateSpecialCost(specialCostInfo);
            if(result > 0){
                return ResponseUtil.success("插入或更新成功", result);
            }else{
                return ResponseUtil.error("用户不存在或插入更新失败");
            }
        }catch (Exception e){
            return ResponseUtil.error("插入或更新失败：" + e.getMessage());
        }
    }
}